<?php

namespace App\Controller;
use App\Services\UserService;
use App\Services\SignService;
use App\Utils\Http;
use App\Services\AesService;
use App\Utils\Sign;


/**
*公众号支付流程
 */

class IndexController{
    public $baseData;

    function __construct()
    {
        $userService = new UserService();

        $this->baseData = $userService->getBaseData();//获取
    }

    /**
    *公众号实名支付(user_id)
     */
    public function gzhPay($cardNumber, $env){
        if($env){
            $domain = "https://payapi.weixiao.qq.com/midas/app";
        }else{
            $domain = "https://payapidev.weixiao.qq.com/midas/app";
        }
        $url = "{$domain}/mppay";
    	$callbackUrl = "https://www.lican.xyz:80/weixiao/callback.php";
//        $callbackUrl = "https://www.higanbana.ltd/index.php/api/pay/callback";
        $arr = [
            'user_id' => $cardNumber,
            'order_id' => date('YmdHis', time()) . mt_rand(100000, 999999),
            'amount' => 1,
            'order_time' => time(),
            'product_id' => 'product001',
            'product_name' => 'goods001',
            'product_detail' => 'detail001',
            'callback_url' => $callbackUrl,
            'metadata' => 'name=xiaoming',
            'order_type' => 0
        ];
        $data = array_merge($arr, $this->baseData);
        $info = SignService::getSign($data, $env);
        Sign::writeLog("请求url：{$url}___请求下单参数：_".__LINE__, $info);
        try{
            $res = Http::post($url, $info);
        }catch(\Exception $e){
            print_r($e->getMessage());die;
        }
	    Sign::writeLog('下单接口返回：_'.__LINE__, $res);
        $payInfo = json_decode($res, true);//得到下单参数
        $payInfo = $payInfo['data']['pay_info'];
        require "./public/H5Pay.html";
    }

    /**
    *非实名支付
     */
    public function noRealPay($openid, $env){
        if($env){
            $domain = "https://payapi.weixiao.qq.com/midas/app";
        }else{
            $domain = "https://payapidev.weixiao.qq.com/midas/app";
        }
        $url = "{$domain}/wxpay";
    	$callbackUrl = "https://www.lican.xyz:80/weixiao/callback.php";
        $arr = [
            'sub_openid' => $openid,
            'order_id' => date('YmdHis', time()) . mt_rand(100000, 999999),
            'amount' => 1,
            'order_time' => time(),
            'product_id' => 'product001',
            'product_name' => 'goods001',
            'product_detail' => 'detail001',
            'callback_url' => $callbackUrl,
            'metadata' => 'name=xiaoming',
            'order_type' => 0
        ];

        $data = array_merge($arr, $this->baseData);
        $info = SignService::getSign($data, $env);
        Sign::writeLog("请求下单url：{$url}__请求下单参数：_".__LINE__, $info);
        $res = Http::post($url, $info);
        Sign::writeLog('下单结果：_'.__LINE__, $res);
        $payInfo = json_decode($res, true);
        $payInfo = $payInfo['data']['pay_params'];
        $appId = $payInfo['appId'];
        $timeStamp = $payInfo['timeStamp'];
        $nonceStr = $payInfo['nonceStr'];
        $package = $payInfo['package'];
        $signType = $payInfo['signType'];
        $paySign = $payInfo['paySign'];
        require "./public/NoRealPay.html";
    }

	public function writeLog($action, $data)

    {
	$data = is_array($data) ? json_encode($data) : $data;

        //设置目录时间

        $day = date('Ymd');



        //设置路径目录信息

        $url = './logs/texlog/' . $day . '/' . $day . '_request_log.txt';



        //取出目录路径中目录(不包括后面的文件)

        $dir_name = dirname($url);



        //如果目录不存在就创建

        if (!file_exists($dir_name)) {

            //iconv防止中文乱码

            $res = mkdir(iconv("UTF-8", "GBK", $dir_name), 0777, true);

        }

        $fp = fopen($url, "a");//打开文件资源通道 不存在则自动创建

        fwrite($fp, var_export(date('Y-m-d H:i:s') . '_' . $action . '_:' . $data, true) . "\r\n");//写入文件

        fclose($fp);//关闭资源通道

    }


}
